//
//  MainViewController.m
//  YKZB
//
//  Created by 曾少伟 on 2017/3/16.
//  Copyright © 2017年 曾少伟. All rights reserved.
//

#import "MainViewController.h"
#import "MainTopView.h"

#define adjustH 113

@interface MainViewController ()<UIScrollViewDelegate,MainTopViewDelegate>

@property (nonatomic,strong)UIScrollView *scrView;
@property (nonatomic,strong)MainTopView *mainTopView;



@property (nonatomic,strong)NSArray *vcNameArr;
@property (nonatomic,strong)NSArray *titlesNameArr;
@end

@implementation MainViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
//    self.edgesForExtendedLayout=UIRectEdgeBottom;

    [self configNavItem];
    [self createSubviews];
    [self configScrView];
    
}
#pragma mark - event
- (void)leftNavItemAction:(UIBarButtonItem *)sender {
    
}

- (void)rightNavItemAction:(UIBarButtonItem *)sender {
    
}

#pragma mark - get
- (NSArray *)vcNameArr {
    if (_vcNameArr == nil) _vcNameArr = @[@"FocusViewController",@"HotViewController",@"NearViewController"];
    return _vcNameArr;
}

- (NSArray *)titlesNameArr {
    if (!_titlesNameArr) _titlesNameArr = @[@"关注",@"热门",@"附近"];
    return _titlesNameArr;
}

#pragma mark - view
- (void)createSubviews {
    self.scrView = ({
        UIScrollView *scrView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 64, ScreenW, ScreenH - adjustH)];
        scrView.pagingEnabled = YES;
        scrView.delegate = self;
        scrView.bounces = NO;
        scrView.contentSize = CGSizeMake(self.vcNameArr.count * ScreenW, ScreenH- adjustH);
        [self.view addSubview:scrView];
        scrView;
    });
    
    for (NSString *vcName in self.vcNameArr) {
        UIViewController *vc = [[NSClassFromString(vcName) alloc]init];
        [self addChildViewController:vc];
    }
    
    self.mainTopView = ({
        MainTopView *mainTopView = [[MainTopView alloc]initWithFrame:CGRectMake(0, 0, 200, 30) titlesNameArr:self.titlesNameArr];
        mainTopView.zDelegate = self;
        self.navigationItem.titleView = mainTopView;
        mainTopView;
    });
    
    
}

- (void)configNavItem {
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"global_search"] style:UIBarButtonItemStyleDone target:self action:@selector(leftNavItemAction:)];
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"title_button_more"] style:UIBarButtonItemStyleDone target:self action:@selector(rightNavItemAction:)];
    [self.navigationItem setLeftBarButtonItem:leftItem];
    [self.navigationItem setRightBarButtonItem:rightItem];
}

- (void)configScrView {
    self.automaticallyAdjustsScrollViewInsets = NO;
    //滚动到第二页
    [self.scrView setContentOffset:CGPointMake(ScreenW, 0) animated:NO];
    //第一次手动去加载代理
    [self scrollViewDidEndScrollingAnimation:self.scrView];
}

#pragma mark - scrview delegate
//两个代理互补
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {
    debugMethod();
    NSInteger idx = scrollView.contentOffset.x/ScreenW;
    
    //头部视图 btn的下面游标的滑动
    NSLog(@"idx~%ld",idx);
    [self.mainTopView scrollToBtn:idx];
    
    UIViewController *vc = self.childViewControllers[idx];
    if ([vc isViewLoaded]) return;
    [self.scrView addSubview:vc.view];
    vc.view.frame = CGRectMake(idx * ScreenW, 0, ScreenW, ScreenH - adjustH);
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    debugMethod();
    [self scrollViewDidEndScrollingAnimation:scrollView];
}

#pragma mark - maintopview zdelegate
- (void)mainTopView:(MainTopView *)mainTopView clickBtn:(NSInteger)idx {
    CGPoint point = CGPointMake(idx *ScreenW, self.scrView.contentOffset.y);
    [self.scrView setContentOffset:point animated:YES];
}

@end
